<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/16
 * Time: 13:27
 */

namespace app\admin\controller;


use app\admin\model\AdminMenu;
use app\admin\model\AdminRole;
use think\Controller;
use think\facade\Session;

class Base extends Controller
{
    protected $url;
    public function initialize(){
//        $access_token = Cache::store('redis')->get('access_token');
//        if (!$access_token){
//            $this->accessToken();
//        }
        $this->url = 'http://'.$_SERVER['HTTP_HOST'];
        if(!Session::get('admin_id')){
            $this->redirect(url("login/login"));
        }
    }
    //todo 权限判断
//    public function auth(){
//        $role_id = Session::get('role_id');
//        $rule = AdminRole::where('id',$role_id)->value('rule');
//        $rule = explode(',',$rule);
//        $controller = request()->controller();
//        $action = request()->action();
//
//        $menu = AdminMenu::where('controller',$controller)
//            ->where('action',$action)->field('id,status')->find();
//        if ($menu){
//            return ['code'=>0,'msg'=>'功能不存在'];
//        }
//        if ($menu['status'] == 0){
//            return ['code'=>0,'msg'=>'功能已禁用'];
//        }
//        if (!in_array($menu,$rule)){
//            return ['code'=>0,'msg'=>'没有权限'];
//        }
//    }
//    public function accessToken(){
//        $ret = WxSet::get(1);
//        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$ret['appID']."&secret=".$ret['secret'];
//        $token = $this->curl($url);
//        if (array_key_exists("access_token",$token)){
//            Cache::store('redis')->set('access_token',$token['access_token'],7150);
//        }
//    }
    //权限数组处理
    public function roleArray($data){
        $results = [];
        foreach ($data as $key => $value) {
            $results[$value['id']] = $value;
        }
        $tree = [];
        foreach ($results as $item) {
            if(isset($results[$item['pid']])){
                $results[$item['pid']]['subset'][] = &$results[$item['id']];
            }else{
                $tree[] = &$results[$item['id']];
            }
        }
        $results = [];
        foreach ($tree as $value) {
            $value['subset'] = isset($value['subset'])?$this->formatMenus($value['subset']):false;
            $results[] = $value;
        }
        return $results;
    }
    private function formatMenus($items,&$res = array()){
        foreach($items as $item){
            if(!isset($item['subset'])){
                $res[] = $item;
            }else{
                $tem = $item['subset'];
                unset($item['subset']);
                $res[] = $item;
                $this->formatMenus($tem,$res);
            }
        }
        return $res;
    }


    //图片上传
    public function upload($file){
        $info = $file->move( '../public/uploads');
        if ($info) {
            $result = [
                'code'     => 1,
                'msg'      => '上传成功',
                'url' => '/uploads/' . str_replace('\\', '/', $info->getSaveName())
            ];
        } else {
            $result = [
                'code' => 0,
                'msg'  => $file->getError()
            ];
        }
        return $result;
    }

}